
//
//  LVImpUserViewController.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/2/28.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVImpUserViewController.h"
#import "UIView+Layer.h"
#import "LVShareCell.h"
#import "LVShareModel.h"
#import "LVImpModel.h"
#import "LVCustomAlertView.h"
#import "LVSettingViewController.h"
#import "LVBellViewController.h"
#import "LVNewPushShareListVc.h"

#define ItemWidth 45.0f
#define PushShareHeight 250.f
@interface LVImpUserViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) LVNewPushShareListVc *pushShareListVc;

@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UIButton *bellBtn;
@property (weak, nonatomic) IBOutlet UIButton *settingBtn;
@property (weak, nonatomic) IBOutlet UIButton *impHeadBtn;
@property (weak, nonatomic) IBOutlet UILabel *impNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgLabel;
@property (weak, nonatomic) IBOutlet UIButton *stateBtn;
@property (weak, nonatomic) IBOutlet UIButton *startShareBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *shareCollectionView;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UILabel *nodataLabel;
@property (weak, nonatomic) IBOutlet UILabel *newsLabel;

@property (weak, nonatomic) IBOutlet UIView *pushShareView;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *pushShareImgViewArr;
@property (weak, nonatomic) IBOutlet UIView *pushShareBtnView;
@property (weak, nonatomic) IBOutlet UIButton *pushShareBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;//分享视图与底部的距离
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *startShareBtnBottom;//开始共享按钮底部距分享视图的距离

- (IBAction)closeAction:(id)sender;
- (IBAction)bellAction:(id)sender;
- (IBAction)settingAction:(id)sender;
- (IBAction)stateAction:(id)sender;
- (IBAction)startShareAction:(id)sender;
- (IBAction)shareNewAction:(id)sender;

@end

@implementation LVImpUserViewController
{
    LVImpModel *_impModel;
    NSMutableArray *_pushShareArr;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(YES),@(0)]];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(NO),@(0)]];

    [self showViewOfNewShareListVc:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeUI];
    [self loadData];
}
#pragma mark markUI
- (void)makeUI
{
    self.view.backgroundColor = KMainColor;

    [_impHeadBtn makeRoundedCorners];
    [_stateBtn makeRoundedCorners];
    [_startShareBtn makeRoundedCorners];
    [_newsLabel makeRoundedCorners];

    [_stateBtn makeBorderColor:[UIColor whiteColor] borderWidth:1];
    [_startShareBtn makeBorderColor:[UIColor whiteColor] borderWidth:2];

    [_pushShareBtnView makeRoundedCorners];
    [_pushShareBtn makeRoundedCorners];
    for (UIView *vi in _pushShareImgViewArr) {
        [vi makeRoundedCorners];
        [vi makeBorderColor:[UIColor whiteColor] borderWidth:1.5];
    }
    UITapGestureRecognizer *tapPush = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPushAction:)];
    [_pushShareView addGestureRecognizer:tapPush];

    _shareCollectionView.delegate = self;
    _shareCollectionView.dataSource = self;

    [_shareCollectionView registerNib:[UINib nibWithNibName:@"LVShareCell" bundle:nil] forCellWithReuseIdentifier:@"LVShareCell"];
}
#pragma mark 懒加载视图
- (LVNewPushShareListVc *)pushShareListVc
{
    if(!_pushShareListVc){
        WeakSelf(weakSelf);
        _pushShareListVc = [[LVNewPushShareListVc alloc] initWithTarget:self close:^(id obj) {
            [weakSelf showViewOfNewShareListVc:NO];
        }];
        _pushShareListVc.view.frame = CGRectMake(0, kScreenHeight, kScreenWidth, PushShareHeight);
    }
    return _pushShareListVc;
}
#pragma mark load
- (void)loadData
{
    _impModel = [[LVImpModel alloc] init];
    for (int i = 0; i < 2; i ++) {
        LVShareModel *sm = [[LVShareModel alloc] init];
        [_impModel.shareModelArr addObject:sm];
    }

    [self refreshState];

    [_shareCollectionView reloadData];
}
#pragma mark refreshUI
- (void)refreshState{
    _stateBtn.hidden = !_impModel.shareModelArr.count;
    _startShareBtn.hidden = _impModel.shareModelArr.count;
    _shareCollectionView.hidden = !_impModel.shareModelArr.count;
    _nodataLabel.hidden = _impModel.shareModelArr.count;
}
//新的分享推送过来啦
- (void)popPushShareView
{
    _pushShareArr = [[NSMutableArray alloc] init];
    [_pushShareArr addObjectsFromArray:_impModel.shareModelArr];
    if (_bottom.constant) {
        //底部新分享视图已经出现
        if (_pushShareArr.count) {
            //如果有新的分享推送过来就要刷新啊
            [self refreshPushShareView];
        }else{
            //隐藏底部新分享视图
            [self showPushShareView:NO];
        }
    }else{
        if (_pushShareArr.count) {
            //如果有新的分享推送过来就要刷新啊并弹出新分享视图啊
            [self refreshPushShareView];

            [self showPushShareView:YES];
        }
    }
}
//是否弹出底部新分享视图
- (void)showPushShareView:(BOOL)isShow
{
    if (isShow) {
        [UIView animateWithDuration:0.5 animations:^{
            _startShareBtnBottom.constant = 20;
            _bottom.constant = 85;
            _pushShareView.alpha = 1;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _startShareBtnBottom.constant = 50;
            _bottom.constant = 0;
            _pushShareView.alpha = 0;
        }];
    }
}
//刷新底部新分享视图
- (void)refreshPushShareView
{
    for (UIView *vi in _pushShareImgViewArr) {
        vi.hidden = YES;
    }
    for (int i = 0; i < _pushShareArr.count; i ++) {
        if(i >= 3) break;
        UIImageView *imgV = _pushShareImgViewArr[i];
        imgV.hidden = NO;
    }
}
//是否显示朋友分享列表
- (void)showViewOfNewShareListVc:(BOOL)isShow
{
    WeakSelf(weakSelf);
    if (isShow) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(NO),@(0)]];

        [self.view addSubview:self.pushShareListVc.view];
        [self.view bringSubviewToFront:self.pushShareListVc.view];

        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.pushShareListVc.view.frame = CGRectMake(0, kScreenHeight - PushShareHeight, kScreenWidth, PushShareHeight);
        }completion:^(BOOL finished) {

        }];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(YES),@(0)]];

        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.pushShareListVc.view.frame = CGRectMake(0, kScreenHeight, kScreenWidth, PushShareHeight);
        }completion:^(BOOL finished) {
            
            [weakSelf showPushShareView:NO];
            [weakSelf.pushShareListVc.view removeFromSuperview];
        }];
    }
}
#pragma mark btnAction
- (IBAction)closeAction:(id)sender {
    WeakSelf(weakSelf);
    if (_closeBlock) {
        [weakSelf showViewOfNewShareListVc:NO];

        _closeBlock(nil);
    }
}
- (IBAction)bellAction:(id)sender {
    LVBellViewController *bellVc = [[LVBellViewController alloc] init];
    [self pushVc:bellVc];
}
- (IBAction)settingAction:(id)sender {
    LVSettingViewController *settingVc = [[LVSettingViewController alloc] init];
    [self pushVc:settingVc];
}
- (IBAction)stateAction:(id)sender {

}
- (IBAction)startShareAction:(id)sender {
    [_shareBtn addShakeAnimationAmplitude:5];
}
- (IBAction)shareNewAction:(id)sender {
    LVShareModel *model = [[LVShareModel alloc] init];
    model.isFail = YES;
    [_impModel.shareModelArr addObject:model];

    [_shareCollectionView reloadData];

    [self refreshState];

    //到最后一个cell
    _shareCollectionView.contentOffset = CGPointMake(ItemWidth * (_impModel.shareModelArr.count - 1), 0);

    //测试哈
    [self popPushShareView];
}
- (void)tapPushAction:(UITapGestureRecognizer *)tap
{
    [self showViewOfNewShareListVc:YES];
}
#pragma mark UICollectionDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _impModel.shareModelArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LVShareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LVShareCell" forIndexPath:indexPath];
    //cell的图片加载
    LVShareModel *sModel = _impModel.shareModelArr[indexPath.row];
    [cell loadCellWithModel:sModel];

    if (sModel.isFail) {
        [cell failWithRetryShareBlock:^(LVShareModel *model, LVShareCell *scell) {
            //重新发送分享而分享成功的操作
            scell.dealView.hidden = YES;
            model.isFail = NO;
        }];
    }else{
        cell.dealView.hidden = YES;
    }
    [cell addLongPressWithBlock:^(LVShareModel *model) {
        LVCustomAlertView *alert = [[LVCustomAlertView alloc] initWithTitle:@"你确定要删除这条此刻吗？" cancel:@"取消" sure:@"确认"];
        [alert showInView:self.view.window];
        WeakSelf(weakSelf);
        [alert setupSureBlock:^BOOL{

            [_impModel.shareModelArr removeObject:model];

            [weakSelf.shareCollectionView reloadData];

            [weakSelf refreshState];

            [weakSelf popPushShareView];
            return YES;
        }];
        [alert setupCancelBlock:^BOOL{

            return YES;
        }];
    }];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ItemWidth, ItemWidth);
}
#pragma mark UICollectionDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LVShareModel *sModel = _impModel.shareModelArr[indexPath.row];

}
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
